package com.example.springboot.ext;

import com.example.springboot.annotion.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SmartInitExtended implements SmartInitializingSingleton, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void afterSingletonsInstantiated() {
        System.out.println("SmartInitExtended afterSingletonsInstantiated");
        Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(User.class);
        beanMap.forEach((k, v) -> {
           if( v instanceof UserInit){
               UserInit userInit = (UserInit) v;
               userInit.setName("ab");
               userInit.setEmail("cd");
           }
        });
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
